package com.itkaien;

import com.itkaien.demoCustomerRegisterBean.UserDao;
import com.itkaien.demoCustomerRegisterBean.UserDaoImpl;

import com.itkaien.service.impl.Student;
import com.itkaien.service.impl.UserServiceimpl;
import java.sql.DriverManager;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.itkaien.springzh.Mybatis.UserService;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    public static void main(String[] args) {

        //1.创建工厂对象

        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

        //2.获取配置文件中定义的对象

        XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);


        //2.1.获取配置文件中定义的对象
        xmlBeanDefinitionReader.loadBeanDefinitions("beans.xml");


        //2.2.根据id获取对象
        Object userService = beanFactory.getBean("userService");
        System.out.println(userService);
        UserServiceimpl userServiceImpl = (UserServiceimpl) beanFactory.getBean("userService");
        System.out.println(userServiceImpl.getUserDao());

        //3.使用对象



        //4.关闭工厂对象


//        方式二

        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        Object userService1 = context.getBean("userService");

        System.out.println(userService1);





    }
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");

        UserService userService = (UserService) context.getBean("userServiceimp");

        System.out.println(userService);

    }

    @Test
    public void testDi() {

        ApplicationContext context = new ClassPathXmlApplicationContext("DIApplication.xml");
        Student student = (Student) context.getBean("myStudent");
        System.out.println(student.toString());


        Student student1 = (Student) context.getBean("myStudentDiRef");
        System.out.println(student1.toString());

        Student myStudent = (Student) context.getBean("myStudent1");
        System.out.println(myStudent.toString());

    }

    @Test
    public void testenv() {

        System.setProperty("spring.profiles.active", "prod");
        ApplicationContext context = new ClassPathXmlApplicationContext("xmllearning.xml");
        Student student = (Student) context.getBean("Stud",Student.class);

        System.out.println(student.toString());

    }

    @Test
    public void testDatasource() {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("DataSource.xml");
        DriverManager dataSource =  context.getBean("connection", DriverManager.class);
        System.out.println(dataSource);

    }


    @Test
    public void testAop() {
        //spring容器的加载过程
        //1.首先加载xml配置文件 在加载配置文件的过程会创建ReaderContext对象 包含一个beanFactory对象  此对象包含每个xml标签中bean的 beanDefinetionMap key是id value是定义的bean标签的各种属性值
        //2.在创建ReaderContext对象的时候会创建一个beanFactory对象 此对象包含每个xml标签中bean的 beanDefinetionMap key是id value是定义的bean标签的各种属性值
        //2.spring会根据beanDefinetionMap中的id值去创建对象 创建对象的时候会根据bean标签中的class属性值去创建对象 创建完对象会存储到beanFactory中的SingletonObjects中



        //自定义处理方法
        //beanFactory.addBeanPostProcessor(new MyBeanPostProcessor());  //Bean工厂后处理器 在beanFinitionMap填充完毕后bean对象创建前执行

        //beanFactory.addBeanPostProcessor(new MyBeanPostProcessor());  //Bean工厂后处理器 在bean创建完成添加到singletonObjects中前执行
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("SpringLifeDay.xml");
        Object springLife = context.getBean("SpringLife");
        System.out.println(springLife);
    }

    @Test
    public void testCustomRegisterBean(){

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("RegesterBean.xml");

//        Object beansFirst = context.getBean("beansFirst");
        UserDao userDao = context.getBean("UserDao",UserDao.class);
        userDao.show();

    }

    @Test
    public void TestAware(){

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Aware.xml");

        Object person = context.getBean("person");


    }

    @Test
    public void TestDatasource(){

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        com.itkaien.springzh.Mybatis.UserService userService = context.getBean("userService",com.itkaien.springzh.Mybatis.UserService.class);
        userService.show();

    }






}
